{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.feature_column import numeric_column\n",
    "from tensorflow.feature_column import categorical_column_with_vocabulary_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "featcols = [\n",
    "tf.feature_column.numeric_column(\"area\"),\n",
    "tf.feature_column.categorical_column_with_vocabulary_list(\"type\",[\"bungalow\",\"apartment\"])\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_input_fn():\n",
    "    features = {\"area\":[1000,2000,4000,1000,2000,4000],\n",
    "              \"type\":[\"bungalow\",\"bungalow\",\"house\",\n",
    "                      \"apartment\",\"apartment\",\"apartment\"]}\n",
    "    labels = [ 500 , 1000 , 1500 , 700 , 1300 , 1900 ]\n",
    "    return features, labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0808 21:52:42.607275 140255449896704 estimator.py:1799] Using temporary folder as model directory: /tmp/tmp_bod1biv\n"
     ]
    }
   ],
   "source": [
    "model = tf.estimator.LinearRegressor(featcols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0808 21:52:42.661280 140255449896704 deprecation.py:323] From /home/am/anaconda3/envs/tf2p0/lib/python3.6/site-packages/tensorflow/python/training/training_util.py:238: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.\n",
      "W0808 21:52:42.708367 140255449896704 deprecation.py:323] From /home/am/anaconda3/envs/tf2p0/lib/python3.6/site-packages/tensorflow/python/ops/lookup_ops.py:1347: to_int64 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "W0808 21:52:42.773968 140255449896704 deprecation.py:323] From /home/am/anaconda3/envs/tf2p0/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/canned/linear.py:300: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "W0808 21:52:42.876165 140255449896704 deprecation.py:506] From /home/am/anaconda3/envs/tf2p0/lib/python3.6/site-packages/tensorflow/python/training/slot_creator.py:187: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow_estimator.python.estimator.canned.linear.LinearRegressorV2 at 0x7f8f440a7e48>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.train(train_input_fn, steps=200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0808 21:52:45.021392 140255449896704 estimator.py:981] Input graph does not use tf.data.Dataset or contain a QueueRunner. That means predict yields forever. This is probably a mistake.\n",
      "W0808 21:52:45.191463 140255449896704 deprecation.py:323] From /home/am/anaconda3/envs/tf2p0/lib/python3.6/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use standard file APIs to check for files with this prefix.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'predictions': array([692.7829], dtype=float32)}\n",
      "{'predictions': array([830.9035], dtype=float32)}\n"
     ]
    }
   ],
   "source": [
    "def predict_input_fn():\n",
    "    features = {\"area\":[1500,1800],\n",
    "              \"type\":[\"house\",\"apartment\"]}\n",
    "    return features\n",
    "\n",
    "predictions = model.predict(predict_input_fn)\n",
    "\n",
    "print(next(predictions))\n",
    "print(next(predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2p0]",
   "language": "python",
   "name": "conda-env-tf2p0-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
